home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.004 / xemacs-1 / xemacs-19.13 / lisp / modes / view-less.el < prev    next >
Encoding:
Text File  |  1995-06-21  |  12.7 KB  |  365 lines

  1. ;;; view-less.el --- Minor mode for browsing files with keybindings like `less'
  2.  
  3. ;; Copyright (C) 1994, 1995 Tinker Systems and INS Engineering Corp.
  4. ;; 
  5. ;; This file is part of XEmacs.
  6. ;; 
  7. ;; XEmacs is free software; you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation; either version 2 of the License, or
  10. ;; (at your option) any later version.
  11. ;; 
  12. ;; XEmacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. ;; GNU General Public License for more details.
  16. ;; 
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with XEmacs; if not, write to the Free Software
  19. ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21. ;;; Synched up with: Not in FSF
  22.  
  23. ;;; Commentary:
  24.  
  25. ;; This mode is for browsing files without changing them.  Keybindings
  26. ;; similar to those used by the less(1) program are used.
  27. ;;
  28. ;; Originally written for v18 by David Gudeman (gudeman@arizona.edu)
  29. ;; Mods by Bengt Martensson, to closely resemble less (July 1987)
  30. ;;
  31. ;; If you would like all write-protected files to be visited in view-mode, 
  32. ;; then add the following to your .emacs file:
  33. ;;
  34. ;;      (add-hook 'find-file-hooks 'auto-view-mode)
  35.  
  36. ;;; Code:
  37.  
  38. (defvar view-search-string ""
  39.   "Last string searched for with view-search functions.")
  40.  
  41. (defvar view-search-arg 1
  42.   "Argument to last view search.")
  43.  
  44. (defvar view-default-lines 10
  45.   "Default value for the \"d\" and \"u\" commands in view-mode")
  46.  
  47. (defvar view-minor-mode nil
  48.   "Non-nil when view-mode is active.  Call `view-mode' to toggle.")
  49. (make-variable-buffer-local 'view-minor-mode)
  50.  
  51. (defvar view-minor-mode-map
  52.   (let ((map (make-keymap)))
  53.     (set-keymap-name map 'view-minor-mode-map)
  54.     (suppress-keymap map)
  55.     (define-key map "-" 'negative-argument)
  56.     (define-key map " " 'scroll-up)
  57.     (define-key map "f" 'scroll-up)
  58.     (define-key map "\177" 'scroll-down)
  59.     (define-key map "b" 'scroll-down)
  60.     (define-key map 'backspace 'scroll-down)
  61.     (define-key map "\r" 'view-scroll-lines-up)
  62.     (define-key map "\n" 'view-scroll-lines-up)
  63.     (define-key map "e" 'view-scroll-lines-up)
  64.     (define-key map "j" 'view-scroll-lines-up)
  65.     (define-key map "y" 'view-scroll-lines-down)
  66.     (define-key map "k" 'view-scroll-lines-down)
  67.     (define-key map "d" 'view-scroll-some-lines-up)
  68.     (define-key map "u" 'view-scroll-some-lines-down)
  69.     (define-key map "r" 'recenter)
  70.     (define-key map "t" 'toggle-truncate-lines)
  71.     (define-key map "N" 'view-buffer)
  72.     (define-key map "E" 'view-file)
  73.     (define-key map "P" 'view-buffer)
  74.     (define-key map "!" 'shell-command)
  75.     (define-key map "|" 'shell-command-on-region)
  76.     (define-key map "=" 'what-line)
  77.     (define-key map "?" 'view-search-backward)
  78.     (define-key map "h" 'view-mode-describe)
  79.     (define-key map "s" 'view-repeat-search)
  80.     (define-key map "n" 'view-repeat-search)
  81.     (define-key map "/" 'view-search-forward)
  82.     (define-key map "\\" 'view-search-backward)
  83.     (define-key map "g" 'view-goto-line)
  84.     (define-key map "G" 'view-last-windowful)
  85.     (define-key map "%" 'view-goto-percent)
  86.     (define-key map "p" 'view-goto-percent)
  87.     (define-key map "m" 'point-to-register)
  88.     (define-key map "'" 'register-to-point)
  89.     (define-key map "C" 'view-cleanup-backspaces)
  90.     (define-key map "\C-c\C-c" 'view-quit)
  91.     ;; #### - should this use substitute-command-keys?
  92.     (define-key map "\C-x\C-q" 'view-quit-toggle-ro)
  93.     (define-key map "q" 'view-quit)
  94.     map
  95.     ))
  96.  
  97. (add-minor-mode 'view-minor-mode " View" view-minor-mode-map)
  98.  
  99. ;;;###autoload
  100. (defun view-file (file &optional other-p)
  101.   "Find FILE, enter view mode.  With prefix arg OTHER-P, use other window."
  102.   (interactive "fView File: \nP")
  103.   (let ((old-p (get-file-buffer file))
  104.     (obuf (current-buffer)))
  105.     (if other-p
  106.     (find-file-other-window file)
  107.       (find-file file))
  108.     (view-mode (if other-p nil obuf)
  109.            (if old-p nil 'kill-buffer))
  110.     nil))
  111.  
  112. ;;;###autoload
  113. (defun view-buffer (buf &optional other-p)
  114.   "Switch to BUF, enter view mode.  With prefix arg use other window."
  115.   (interactive "bView Buffer: \nP")
  116.   (let ((obuf (current-buffer)))
  117.     (if other-p
  118.     (switch-to-buffer-other-window buf)
  119.       (switch-to-buffer buf))
  120.     (view-mode (if other-p nil obuf) (if other-p nil 'bury-buffer))))
  121.  
  122. ;;;###autoload
  123. (defun view-file-other-window (file)
  124.   "Find FILE in other window, and enter view mode."
  125.   (interactive "fView File: ")
  126.   (view-file file t))
  127.  
  128. ;;;###autoload
  129. (defun view-buffer-other-window (buffer)
  130.   "Switch to BUFFER in another window, and enter view mode."
  131.   (interactive "bView Buffer: ")
  132.   (view-buffer buffer t))
  133.  
  134. (defun view-brief-help ()
  135.   (message
  136.    (substitute-command-keys
  137.     "\\<view-minor-mode-map>\\[scroll-up] = page forward; \\[scroll-down] = page back; \
  138. \\[view-mode-describe] = help; \\[view-quit] = quit.")))
  139.  
  140. (defvar unview-position)
  141. (defvar unview-prev-buffer)
  142. (defvar unview-exit-action)
  143. (defvar unview-read-only)
  144.  
  145. ;;;###autoload
  146. (defun view-minor-mode (&optional prev-buffer exit-action)
  147.   "Minor mode for viewing text, with bindings like `less'.
  148. Commands are:
  149. \\<view-minor-mode-map>
  150. 0..9    prefix args
  151. -    prefix minus
  152. \\[scroll-up]    page forward
  153. \\[scroll-down]    page back
  154. \\[view-scroll-lines-up]    scroll prefix-arg lines forward, default 1.
  155. \\[view-scroll-lines-down]    scroll prefix-arg lines backward, default 1.
  156. \\[view-scroll-some-lines-down]    scroll prefix-arg lines backward, default 10.
  157. \\[view-scroll-some-lines-up]    scroll prefix-arg lines forward, default 10.
  158. \\[what-line]    print line number
  159. \\[view-mode-describe]    print this help message
  160. \\[view-search-forward]    regexp search, uses previous string if you just hit RET
  161. \\[view-search-backward]    as above but searches backward
  162. \\[view-repeat-search]    repeat last search
  163. \\[view-goto-line]    goto line prefix-arg, default 1
  164. \\[view-last-windowful]    goto line prefix-arg, default last line
  165. \\[view-goto-percent]    goto a position by percentage
  166. \\[toggle-truncate-lines]    toggle truncate-lines
  167. \\[view-file]    view another file
  168. \\[view-buffer]    view another buffer
  169. \\[view-cleanup-backspaces]    cleanup backspace constructions
  170. \\[shell-command]    execute a shell command
  171. \\[shell-command-on-region]\
  172.     execute a shell command with the region as input
  173. \\[view-quit]    exit view-mode, and bury the current buffer.
  174.  
  175. If invoked with the optional (prefix) arg non-nil, view-mode cleans up
  176. backspace constructions.
  177.  
  178. More precisely:
  179. \\{view-minor-mode-map}"
  180.   (interactive)
  181.  
  182.   (make-local-variable 'view-default-lines)
  183.   (set (make-local-variable 'unview-position)      (point))
  184.   (set (make-local-variable 'unview-prev-buffer)   prev-buffer)
  185.   (set (make-local-variable 'unview-exit-action)   exit-action)
  186.   (set (make-local-variable 'unview-read-only)     buffer-read-only)
  187.   (add-hook (make-local-variable 'change-major-mode-hook)
  188.         'view-fixup-read-only)
  189.   (setq view-minor-mode  t
  190.     buffer-read-only t)
  191.   (view-brief-help))
  192.  
  193. ;;;###autoload
  194. (defun view-mode (&optional prev-buffer exit-action clean-bs)
  195.   "View the current buffer using view-minor-mode.  This exists to be 99.9%
  196. compatible with the implementations of `view-mode' in view.el and older
  197. versions of view-less.el."
  198.   (interactive (list nil 'bury-buffer current-prefix-arg))
  199.   ;; #### - The first two arguments provide compatibility with view.el (and
  200.   ;; thus FSFmacs), while the third argument as a prefix argument maintains
  201.   ;; interactive compatibility with older versions of view-less.  --Stig
  202.   (if clean-bs (cleanup-backspaces))
  203.   (view-minor-mode prev-buffer exit-action))
  204.  
  205. ;;;###autoload immediate
  206. (defun auto-view-mode ()
  207.   "If the file of the current buffer is not writable, call view-mode.
  208. This is meant to be added to `find-file-hooks'."
  209.   (or (file-writable-p buffer-file-name)
  210.       (view-minor-mode)))
  211.  
  212. (defun view-fixup-read-only ()
  213.   ;; doing M-x normal mode should NOT leave the buffer read-only
  214.   (and (boundp 'unview-read-only)
  215.        (progn (setq buffer-read-only unview-read-only)
  216.           (kill-local-variable 'unview-read-only))))
  217.  
  218. (defun view-quit-toggle-ro ()
  219.   "Exit view mode and execute the global binding of the key that invoked this
  220. command.  Normally, this will toggle the state of `buffer-read-only', perhaps
  221. invoking some version-control mechanism."
  222.   (interactive) 
  223.   (setq unview-position nil)
  224.   (view-quit t)
  225.   ;; no longer in view-minor-mode, so the keymap has changed...
  226.   (call-interactively (key-binding (this-command-keys))))
  227.  
  228. (defun view-quit (&optional no-exit-action)
  229.   "Exit view mode.  With prefix argument, keep the current buffer selected."
  230.   (interactive "P")
  231.   (view-fixup-read-only)
  232.   (setq view-minor-mode nil)
  233.   (if unview-position (goto-char unview-position))
  234.   (let ((pbuf unview-prev-buffer)
  235.     (exitact unview-exit-action))
  236.     (if no-exit-action
  237.     nil
  238.       (if exitact (funcall exitact (current-buffer)))
  239.       (if pbuf (switch-to-buffer pbuf)))))
  240.  
  241. ;; #### - similar to what's in man.el and this ought to be written in C anyway...  --Stig
  242. (defun cleanup-backspaces ()
  243.   "Cleanup backspace constructions.
  244. _^H and ^H_ sequences are deleted.  x^Hx sequences are turned into x for all
  245. characters x.  ^^H| and |^H^ sequences are turned into ^.  +^Ho and o^H+ are
  246. turned into (+)."
  247.   (interactive)
  248.   (save-excursion
  249.     (goto-char (point-min))
  250.     (while (= (following-char) ?\C-h)
  251.       (delete-char 1))
  252.     (while (search-forward "\C-h" nil t)
  253.       (forward-char -2)
  254.       (cond ((looking-at "_\C-h\\|\\(.\\)\C-h\\1\\||\C-h\\^")
  255.          (delete-char 2))
  256.         ((looking-at ".\C-h_\\|\\^\C-h|")
  257.          (forward-char 1)
  258.          (delete-char 2))
  259.         ((looking-at "+\C-ho\\|o\C-h+")
  260.          (delete-char 3)
  261.          (insert "(+)"))
  262.         ((looking-at "|\C-h-")
  263.          (delete-char 3)
  264.          (insert "*"))
  265.         (t (forward-char 2))))))
  266.  
  267. (defun view-cleanup-backspaces ()
  268.   "Cleanup backspaces and if buffer is currently unmodified, don't flag it
  269. as a modified buffer.  This works even if the buffer is read-only."
  270.   (interactive)
  271.   (let ((buffer-read-only)
  272.     (buf-mod (buffer-modified-p)))
  273.     (cleanup-backspaces)
  274.     ;; #### - THIS IS PROBABLY A REALLY DANGEROUS THING TO DO IN A MINOR MODE!!
  275.     (set-buffer-modified-p buf-mod)))
  276.  
  277. (defun toggle-truncate-lines (&optional p)
  278.   "Toggles the values of truncate-lines.
  279. Positive prefix arg sets, negative disables."
  280.   (interactive "P")
  281.   (setq truncate-lines (if p
  282.                (> (prefix-numeric-value p) 0)
  283.              (not truncate-lines)))
  284.   (recenter))
  285.  
  286. (defun view-scroll-lines-up (p)
  287.   "Scroll up prefix-arg lines, default 1."
  288.   (interactive "p")
  289.   (scroll-up p))
  290.  
  291. (defun view-scroll-lines-down (p)
  292.   "Scroll down prefix-arg lines, default 1."
  293.   (interactive "p")
  294.   (scroll-up (- p)))
  295.  
  296. (defun view-scroll-some-lines-down (&optional n)
  297.   "Scroll down prefix-arg lines, default 10, or last argument."
  298.   (interactive "p")
  299.   (if (> n 1) (setq view-default-lines n))
  300.   (scroll-down view-default-lines))
  301.  
  302. (defun view-scroll-some-lines-up (&optional n)
  303.   "Scroll up prefix-arg lines, default 10, or last argument."
  304.   (interactive "p")
  305.   (if (> n 1) (setq view-default-lines n))
  306.   (scroll-up view-default-lines))
  307.  
  308. (defun view-goto-line (&optional n)
  309.   "Goto prefix arg line N.  N = 1 by default.."
  310.   (interactive "p")
  311.   (goto-line n))
  312.  
  313. (defun view-last-windowful (&optional n)
  314.   "Goto prefix arg line N or the first line of the last windowful in buffer."
  315.   (interactive "p")
  316.   (if current-prefix-arg
  317.       (goto-line n)
  318.     (end-of-buffer)
  319.     (recenter -1)
  320.     (move-to-window-line 0)))
  321.  
  322. (defun view-goto-percent (&optional percent)
  323.   "Set mark and go to a position PERCENT way into the current buffer."
  324.   (interactive "p")
  325.   (set-mark-command nil)
  326.   (goto-char (+ (point-min) (/ (* percent (- (point-max) (point-min))) 100)))
  327.   (beginning-of-line))
  328.  
  329. (defun view-mode-describe ()
  330.   (interactive)
  331.   (let ((mode-name "View")
  332.     (major-mode 'view-mode))
  333.     (describe-mode)))
  334.  
  335. (defun view-search-forward (s p)
  336.   "Search forward for REGEXP.  If regexp is empty, use last search string.
  337. With prefix ARG, search forward that many occurrences."
  338.   (interactive "sView search: \np")
  339.   (unwind-protect
  340.       (re-search-forward        
  341.        (if (string-equal "" s) view-search-string s) nil nil p)
  342.     (setq view-search-arg p)
  343.     (or (string-equal "" s)
  344.     (setq view-search-string s))))
  345.  
  346. (defun view-search-backward (s p)
  347.   "Search backward for REGEXP.  If regexp is empty, use last search string.
  348. With prefix ARG, search forward that many occurrences."
  349.   (interactive "sView search backward: \np")
  350.   (view-search-forward s (- p)))
  351.  
  352. (defun view-repeat-search (p)
  353.   "Repeat last view search command.  If a prefix arg is given, use that
  354. instead of the previous arg, if the prefix is just a -, then take the
  355. negative of the last prefix arg."
  356.   (interactive "P")
  357.   (view-search-forward
  358.    view-search-string
  359.    (cond ((null p) view-search-arg)
  360.      ((eq p '-) (- view-search-arg))
  361.      (t (prefix-numeric-value p)))))
  362.  
  363. (provide 'view)
  364. (provide 'view-less)
  365.